var cardDingdTable;
var lifeDdStates =
{
	'0' : '未处理',
	'1' : '待提',
	'2' : '完成',
	'3' : '作废',
	'4' : '<font color=#f00>超期</font>'
};
var lifeDdStates2 =
{
    '0' : '待发货',
    '1' : '待收货',
    '2' : '完成',
    '3' : '作废',
    '4' : '<font color=#f00>超期</font>'
};

var buyTypes={"1":"送货上门","2":"门店自取"}
/****列表信息*/
function showDingDanLife()
{
	showLoading("查询中...");
	$.ajax(
	{
		url : 'cardDingdan/initQuery.do',
		type : 'get',
		dataType : 'html',
		async : true,
		success : function(html)
		{
			$('#mainForm').html(html);
			queryDdLifeDatas();
			hideLoading();
			//绑定查询按钮 
		$('#searchDingDanBtn').click(function()
		{
			queryDdLifeList();
		});
		$(".comboBox").kendoComboBox(
		{
			filter : "contains",
			suggest : true
		});
	}
	});
}
function queryDdLifeDatas()
{
	cardDingdTable = $('#entTable').table(
	{
		width : '100%',
		form :
		{
			url : 'cardDingdan/queryList.do',
			id : '#entSearchForm',
			type : 'post',
			pageNumberText : '#pageNum'
		},
		pageable : true,
		selectable : true,
		colums : [
		{
			columnName : '订单编号',
			name : 'dingDanUid'
		},
		{
            columnName : '收货人',
            name : 'kehuName',
            sortable : false
        },
        {
            columnName : '客户电话',
            name : 'lxTel',
            sortable : false
        },
        {
            columnName : '收货地址',
            name : 'address',
            sortable : false
        },
		{
			columnName : '门店',
			name : 'mendName',
			sortable : false
		},
		{
			columnName : '购买方式',
			name : 'buyType',
			formatter : function(obj)
            {
                return buyTypes[obj.buyType];
            },
			sortable : false
		},
		{
			columnName : '服务站',
			name : 'fwzName',
			sortable : false
		},
				{
					columnName : '订单状态',
					name : 'state',
					formatter : function(obj)
					{
			if("1"==obj.buyType){
				return lifeDdStates2[obj.state];
			}else{
				return lifeDdStates[obj.state];
			}	
					}
				},
				{
					columnName : '操作',
					width : "60px",
					name : '',
					sortable : false,
					formatter : function(obj)
					{
						return '<input type="button" value="查看"   style="cursor: pointer" onclick="toAddDdLife(' + obj.id + ',\'' + obj.buyType + '\',' + obj.state + ')">'
					}
				}]
	});
}
function queryDdLifeList()
{
	showLoading("查询中...");
	queryDdLifeDatas();
	hideLoading();
}
function openCardDdDialog(st)
{
	$("#hiddenState").val(st);
	$.get('cardDingdan/zhipai.do', function(html)
	{
		$('#kuDialogDiv').dialog(
		{
			modal : true,
			title : '请选择门店',
			width : 755,
			height : 460,
			open : function(event, ui)
			{
				$('#kuDialogDiv').html(html);
				queryZhipaiMendian2();
			},
			close : function(event, ui)
			{
				//
			$('#kuDialogDiv').dialog("destroy");
			$('#kuDialogDiv').remove();
			$("#parentDiv").html("<div id='kuDialogDiv' style='display: none'></div>");
		}
		});
	});
}

function queryZhipaiMendian2()
{
	$('#entTable2').table(
			{
				width : '100%',
				form :
				{
					url : 'menDian/queryMendianPageList.do',
					id : '#entSearchForm1',
					type : 'post',
					pageNumberText : "#pageNum"
				},
				pageable : true,
				selectable : false,
				colums : [
						{
							columnName : '编号',
							width : '25px',
							name : 'id',
							sortable : false
						},
						{
							columnName : '门店名称',
							width : '130px',
							name : 'name',
							sortable : false
						},
						{
							columnName : '地址',
							width : '160px',
							name : 'address',
							sortable : false
						},
						{
							columnName : '联系人',
							width : '40px',
							name : 'lxr',
							sortable : false
						},
						{
							columnName : '联系电话',
							width : '100px',
							name : 'lxrTel',
							sortable : false
						},
						{
							columnName : '操作',
							name : '',
							width : '40px',
							sortable : false,
							formatter : function(obj)
							{
								return "<input type='button' style='cursor: pointer' value='选择' onclick='zhipaiMengDian2(" + obj.id + ",\"" + obj.address + "\",\"" + obj.name
										+ "\",\"" + obj.lxr + "\")'>";
							},
							sortable : false
						}]
			});
}

//动态加载门店数据
function zhipaiMengDian2(id, dizhi, name, lxr)
{
	$('#mendianIds').val(id);
	$('#state1').html("已指派");
	$('#kuDialogDiv').dialog('close');
	
	$('#kuDialogDiv').remove();
	$('#DingDanForm').ajaxSubmit(
	{
		url : 'cardDingdan/saveDingDan.do',
		type : 'post',
		dataType : 'json',
		success : function(data)
		{
			if (data.success)
			{
			showDialog('操作成功');
			$('#commonDialogDiv').dialog('close');
			cardDingdTable.refresh();
			}
			else
			{
			showDialog('保存失败' + data.message);
			}
		}
	});
}
//去查看
function toSearchDingDan(id)
{
	var param = "";
	if (id)
	{
	param = '?id=' + id;
	}
	$.get('cardDingdan/toAddDdLife.do' + param, function(html)
	{
		$('#commonDialogDiv').html(html);
		$('#commonDialogDiv').dialog(
		{
			modal : true,
			title : '订单信息表',
			width : 900,
			height : 600,
			buttons :
			{
				'取消' : function()
				{
					$(this).dialog('close');
				}
			},
			close : function()
			{
				$(this).dialog('destroy');
			}
		});
	});
}
//去增加
function toAddDdLife(id, genghuan, state)
{
	var param = '?s=' + ts();
	if (id)
	{
	param += '&id=' + id;
	}
	$.get('cardDingdan/toAddDdLife.do' + param, function(html)
	{
		$('#commonDialogDiv').html(html);
		if ('2' == state || '3' == state)
		{//已经完成的只能查看
			btns =
			{
				'取消' : function()
				{
					$(this).dialog('close');
				}
			};
			}
			else
			{
			if ('2' == (genghuan.trim()))
			{
			btns =
			{
				'待提货' : function()
                {
                    $("#hiddenState").val('1');
                    saveAddDdLife();
                },
				'作废' : function()
				{
					$("#hiddenState").val('3');
					saveAddDdLife();
				},
				'完成' : function()
                {
                    $("#hiddenState").val('2');
                    saveAddDdLife();
                },
				'取消' : function()
				{
					$(this).dialog('close');
				}
			};
			}
			else
			{
			btns =
			{
//				'指派' : function()
//				{
//					openCardDdDialog('2');
//				},
				'待收货' : function()
                {
                    $("#hiddenState").val('1');
                    saveAddDdLife();
                },
				'作废' : function()
				{
					$("#hiddenState").val('3');
					saveAddDdLife();
				},
				'完成' : function()
                {
                    $("#hiddenState").val('2');
                    saveAddDdLife();
                },
				'取消' : function()
				{
					$(this).dialog('close');
				}
			};
			}
			}
			$('#commonDialogDiv').dialog(
			{
				modal : true,
				title : '订单信息表',
				width : 900,
				height : 600,
				buttons : btns,
				close : function()
				{
					$(this).dialog('destroy');
				}
			});
		});
}
//去保存（改状态）
function saveAddDdLife()
{
	$('#DingDanForm').ajaxSubmit(
	{
		url : 'cardDingdan/saveDingDan.do',
		type : 'post',
		dataType : 'json',
		success : function(data)
		{
			if (data.success)
			{
			showDialog('处理成功');
			$('#commonDialogDiv').dialog('close');
			cardDingdTable.refresh();
			}
			else
			{
			showDialog('保存失败' + data.message);
			}
		}
	});
}
//批量删除 
function toDeleteDdLife()
{
	var ids = '';
	$('.ids').each(function()
	{
		if (this.checked == true)
		{
		ids += 'ids=' + this.value + '&';
		}
	});
	if (ids.length == 0)
	{
	showDialog('请至少选择一条记录！');
	return;
	}
	$('#commonDialogDiv').html('<p>确定要删除记录信息？</p>');
	$('#commonDialogDiv').dialog(
	{
		modal : true,
		width : 250,
		height : 160,
		buttons :
		{
			'确定' : function()
			{
				$(this).dialog('close');
				$.ajax(
				{
					url : 'cardDingdan/delete.do?' + ids,
					type : 'get',
					dataType : 'json',
					success : function(data)
					{
						if (data.success)
						{
						cardDingdTable.refresh();
						}
						else
						{
						showDialog('删除失败，原因：' + data.message);
						}
					}
				});
			},
			'取消' : function()
			{
				$(this).dialog('close');
			}
		},
		close : function()
		{
			$(this).dialog('destroy');
		}
	});
}

function exportDdLifeExcel()
{
	$("#hDingDanUid").val($("#disDingDanUid").val());
	$("#hState").val($("#disState").val());
	$("#hChangeType").val($("#disChangeType").val());
	$("#dingDanExcelForm").attr("action", "cardDingdan/exportExcel.do").submit();
}

//去查看
function toSearchDdLife(id)
{
	var param = "";
	if (id)
	{
	param = '?id=' + id;
	}
	$.get('cardDingdan/toAddDdLife.do' + param, function(html)
	{
		$('#commonDialogDiv').html(html);
		$('#commonDialogDiv').dialog(
		{
			modal : true,
			title : '订单信息表',
			width : 900,
			height : 600,
			buttons :
			{
				'取消' : function()
				{
					$(this).dialog('close');
				}
			},
			close : function()
			{
				$(this).dialog('destroy');
			}
		});
	});
}
